/*
 * @Author: Donald duck tang5722917@163.com
 * @Date: 2024-06-27 19:21:45
 * @LastEditors: Donald duck tang5722917@163.com
 * @LastEditTime: 2024-06-28 18:02:38
 * @FilePath: \mysticism-mud\lpc_atest\lpc_atest_log.c
 * @Description:  log output function
 * Copyright (c) 2024 by Donald duck email: tang5722917@163.com, All Rights Reserved.
 */
#include "lpc_atest.h"

//log文件路径
#ifndef ATEST_LOG_FILE
#define ATEST_LOG_FILE   PATH_DIR "log/atest.log"
#endif


int test_result_check(mixed * test_result){
    Test_Info info;
    for(int i=1; i<sizeof(test_result); i += 2)
    {
        info = test_result[i];
        if (info->result == 0) return 0; 
        else if (info->result == -1) return -1; 
    }
    return 1;
}

string test_result_log(int result){
    if( result == 1) return "ALL PASS";
    else if ( result == -1) return "Partially PASS";
    else return "Error";
}

int write_log(mixed * test_result,int rank){
    string log_str="";
    int test_num = 1;
    int result = test_result_check(test_result);
    int info_num = sizeof(test_result);
    Test_Info info;
    
    if(file_size(ATEST_LOG_FILE) == -1)
    {
        cp(LPC_ATEST_PATH PATH_DIR+"lpc_atest.c",ATEST_LOG_FILE);
        debug_message("Not find log file");
    }
    log_str +="                   ATEST Unite Test Report                  \n";
    log_str +="************************************************************\n";
    log_str += "测试日期： "+TIME_D->real_time_description() + "\n";
    log_str +="************************************************************\n";
    log_str += "测试结果： "+ test_result_log(result) + "\n";
    log_str +="************************************************************\n";
    for(int i=1; i<info_num; i += 2)
    {    
        info = test_result[i];
        if((rank > 1) || (info->result != 1))    
        {        
            log_str +="第"+ test_num +"项测试内容： "+ (info->info ? info->info : "无测试说明") +"\n";
            log_str +="测试文件路径： "+ test_result[i-1]+"\n";
            log_str +="测试数量： "+ sizeof(info->result_info)/3+ "  " + test_result_log(info->result) + "\n";
            for(int j=0; j<sizeof(info->result_info); j += 3){
                log_str += "【"+info->result_info[j]+"】";
                if(info->result_info[j+1] == 1 ) log_str += "TEST PASS\n";
                else if(info->result_info[j+1] == 0 ) log_str +="TEST Fail :"+ info->result_info[j+2] +"\n";
                else log_str +="TEST Error\n";
            }
            log_str +="************************************************************\n";
            test_num += 1;
        }
    }
    if(rank > 2){    
        log_str +="************************************************************\n";
        log_str +="Test Info DATA : \n";
        log_str += sprintf("%O",test_result); }
    return write_file(ATEST_LOG_FILE,log_str,1);
}
